package cn.loioi.multi.mysql.sql;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;

public class SelectBaseArea {
	/**
	 * 专为select2 插件生成的
	 * @param map
	 * @return
	 */
	public String selectForSelect2(Map<String, Object> map) {
		String code = getString(map.get("code"));
		String name = getString(map.get("name"));
		// 查询语句
		String sql = "SELECT base_area.`code` AS id, base_area.`name` AS text FROM base_area WHERE code LIKE #{_code}";
		// 查询使用的code
		if (StringUtils.isEmpty(code)) {
			map.put("_code", "__");
		} else if (code.length() == 2 || code.length() == 4) {
			map.put("_code", code + "__");
		} else {
			map.put("_code", code + "___");
		}
		// 模糊查找名字
		if (StringUtils.isNotEmpty(name)) {
			map.put("_name", "%" + name + "%");
			sql += " AND name LIKE #{_name}";
		}
		return sql;
	}

	private String getString(Object obj) {
		return obj == null ? null : obj.toString();
	}
}
